package com.huawei123.hasee.cniao5store.mvp.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.huawei123.hasee.cniao5store.R;
import com.huawei123.hasee.cniao5store.adapter.RecommendAdapter;
import com.huawei123.hasee.cniao5store.adapter.top.RecommendTopWrapper;
import com.huawei123.hasee.cniao5store.base.BaseMvpFragment;
import com.huawei123.hasee.cniao5store.bean.RecommendBean;
import com.huawei123.hasee.cniao5store.mvp.presenter.impl.RecommendPresenterImpl;
import com.huawei123.hasee.cniao5store.mvp.view.acyivity.AppDetailActivity;
import com.huawei123.hasee.cniao5store.mvp.view.view.RecommendFragmentView;
import com.huawei123.hasee.cniao5store.utils.UIUtils;
import com.huawei123.hasee.cniao5store.view.LoadingPager;
import com.zhxu.recyclerview.pullrefresh.PullToRefreshView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称：Cniao5Store
 * 类描述：推荐Fragment
 * 创建人：Administrator
 * 创建时间：2017/10/25 0025 下午 4:56
 * 修改人：Administrator
 * 修改时间：2017/10/25 0025 下午 4:56
 * 修改备注：
 */

public class RecommendFragment extends BaseMvpFragment<RecommendPresenterImpl> implements RecommendFragmentView {
    @BindView(R.id.mRV_recommend)
    RecyclerView mRV_recommend;
    @BindView(R.id.mPTR)
    PullToRefreshView mPTRV;

    @Inject
    public RecommendPresenterImpl recommendPresenterimpl;
    private View view;
    private RecommendBean recommendBean;
    private RecommendAdapter recommendAdapter;
    private RecommendTopWrapper recommendTopWrapper;


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        show();//在第一次进入时加载第一个界面需手动调用show();
    }

    @Override
    protected void loading() {
        recommendPresenterimpl.getRecommendData(myActivity);

    }


    /**
     * 创建请求成功后加载的界面（在下面requestDataSuccess后执行）
     *
     * @return
     */
    @Override
    protected View createSuccessView() {
        /*FrameLayout中需要加载的View*/
        //        TextView textView = new TextView(getContext());
        //        textView.setText("RecommendFragment加载成功");
        view = UIUtils.inflate(R.layout.fragment_recommend);
        ButterKnife.bind(this, view);
        //初始化整个推荐页布局
        mRV_recommend.setLayoutManager(new LinearLayoutManager(getContext()));
        recommendAdapter = new RecommendAdapter(getContext(), recommendBean.getRecommendAppBeanList());
        recommendAdapter.setAppItemListener(new RecommendAdapter.AppItemListener() {
            @Override
            public void getAppName(String name) {
                Intent intent = new Intent(myActivity, AppDetailActivity.class);
                intent.putExtra("packageName",name);
                startActivity(intent);
            }
        });
        //将初始化成功的推荐页布局传入添加头部滚动条的封装中，在里面完成布局添加头部滚动条
        recommendTopWrapper = new RecommendTopWrapper(getContext(), recommendAdapter);
        recommendTopWrapper.addDataAll(recommendBean.getBannerList());
        //recyclerView这时用的适配器是添加过头部滚动条的封装对象recommendTopWrapper，不是最初初始化的recommendAdapter适配器
        mRV_recommend.setAdapter(recommendTopWrapper);
        /*设置上拉加载监听*/
        mPTRV.setPullDownEnable(false);//关闭下拉刷新
        mPTRV.setListener(new PullToRefreshView.OnRefreshListener() {
            @Override//刷新回调
            public void onRefresh() {

            }

            @Override//加载回调
            public void onLoadMore() {
                recommendPresenterimpl.getRecommendDataMore(myActivity);
            }
        });
        return view;
    }

    /**
     * 请求数据成功
     *
     * @param recommendBean
     */
    List<RecommendBean.RecommendAppBean> recommendBeanList = new ArrayList<>();

    @Override
    public void requestDataSuccess(RecommendBean recommendBean) {
        this.recommendBean = recommendBean;
        setState(LoadingPager.LoadResult.success);
    }

    @Override
    public void requestDataError(String errmsg) {
        setState(LoadingPager.LoadResult.error);
    }

    /**
     * 上拉加载请求成功返回
     *
     * @param recommendBean
     */
    @Override
    public void requestMoreRecommend(RecommendBean recommendBean) {
        recommendAdapter.clearData();
        recommendAdapter.addDataAll(recommendBean.getRecommendAppBeanList());
        recommendAdapter.notifyDataSetChanged();
        mPTRV.onFinishLoading();
    }

    @Override
    public void requestDataEmpty() {
        setState(LoadingPager.LoadResult.empty);
    }

    @Override
    protected RecommendPresenterImpl initPresenterInjector() {
        //使用父类BaseMvpFragment实例化好的ragmentComponent对象注入本Fragment对象
        fragmentComponent.inject(this);
        //返回注入的Presenter
        return recommendPresenterimpl;
    }
}
